package com.hotel.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotel.auto.JSONAuthentication;
import com.hotel.entity.HoHouse;
import com.hotel.entity.HoPass;
import com.hotel.entity.HoPrice;
import com.hotel.mapper.HoHouseMapper;
import com.hotel.mapper.HoPassMapper;
import com.hotel.mapper.HoPriceMapper;
import com.hotel.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author animal
 * @since 2022-02-27
 */
@RestController
@RequestMapping("/ho-price")
public class HoPriceController extends JSONAuthentication {
    @Autowired
    HoPriceMapper hoPriceMapper;
    @Autowired
    HoHouseMapper hoHouseMapper;
    @Autowired
    HoPassMapper hoPassMapper;

    @ApiOperation(value = "添加财务报表")
    @PostMapping("/add")
    public void add(HttpServletRequest request, HttpServletResponse response, @RequestBody HoPrice hoPrice) throws Exception{
        if(hoPriceMapper.insert(hoPrice)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }

    }
    @ApiOperation(value = "查看财务报表")
    @PostMapping("/look")
    public void add(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int  size,@RequestParam("current") int current) throws Exception{
        Page<HoPrice> page=new Page<>(current,size);
        Page<HoPrice> pages=hoPriceMapper.selectPage(page,null);
        List<HoPrice> list = pages.getRecords();
        int total=hoPriceMapper.selectCount(null);
        for(HoPrice a:list){
            if(a.getUserId()!=null){
                a.setHoPass(hoPassMapper.selectById(a.getUserId()));
            }
            if(a.getHoId()!=null){
                a.setHoHouse(hoHouseMapper.selectById(a.getHoId()));
            }
        }
        if(list.size()>0){
            Result ok = Result.ok().data("price",list).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error().message("暂无财务报表");
            this.WriteJSON(request, response, error);
        }
    }

}

